package com.xilihui.xlh.component.recyclerview;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xilihui.xlh.core.glide.ImageHelper;


public class BaseViewHolder extends RecyclerView.ViewHolder {

    public BaseViewHolder(View itemView) {
        super(itemView);
    }

    public void setImage(Context context, @IdRes int viewId, String url) {
        ImageView img = (ImageView) itemView.findViewById(viewId);
        ImageHelper.display((Activity) context, img, url);
    }

    public void setImageResource(@IdRes int viewId, @DrawableRes int imageRes) {
        ImageView img = (ImageView) itemView.findViewById(viewId);
        img.setImageResource(imageRes);
    }

    public void setText(@IdRes int viewId, String text) {
        ((TextView) itemView.findViewById(viewId)).setText(text);
    }

    public void setText(@IdRes int viewId, SpannableStringBuilder text) {
        ((TextView) itemView.findViewById(viewId)).setText(text);
    }

    public void setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
        itemView.findViewById(viewId).setOnClickListener(listener);
    }

    public void setOnLongClickListener(@IdRes int viewId, View.OnLongClickListener listener) {
        itemView.findViewById(viewId).setOnLongClickListener(listener);
    }

    public void setVisibility(int viewId, int visibility) {

        itemView.findViewById(viewId).setVisibility(visibility);
    }

    public void setRating(@IdRes int viewId, float rating) {

        ((RatingBar) itemView.findViewById(viewId)).setRating(rating);
    }

    public View getView(@IdRes int resId) {

        return itemView.findViewById(resId);
    }

    public String getText(@IdRes int resId) {
        TextView textView = (TextView) itemView.findViewById(resId);
        return textView.getText().toString().trim();
    }

    public void setTextWatcher(@IdRes int resId, TextWatcher watcher) {
        EditText editText = (EditText) itemView.findViewById(resId);
        editText.addTextChangedListener(watcher);
    }

    public void setFilter(@IdRes int resId, InputFilter filter) {
        EditText editText = (EditText) itemView.findViewById(resId);
        editText.setFilters(new InputFilter[]{filter});
    }

    public void setTextColor(Context context, @IdRes int viewId, @ColorRes int colorRes) {
        ((TextView) itemView.findViewById(viewId)).setTextColor(context.getResources().getColor(colorRes));
    }

    public void setTextSize(@IdRes int viewId, int size) {
        ((TextView) itemView.findViewById(viewId)).setTextSize(size);
    }

    public void setCompoundRightRes(Context context, @IdRes int viewId, @DrawableRes int right) {
        TextView textView = (TextView) itemView.findViewById(viewId);
        Drawable drawable = context.getResources().getDrawable(right);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);

    }

    public void setProgressNoAnim(@IdRes int viewId, int progress) {
        ProgressBar progressBar = (ProgressBar) itemView.findViewById(viewId);
        progressBar.setMax(100);
        progressBar.setProgress(progress);
    }

    public void setProgress(@IdRes int viewId, int progress) {
        ProgressBar progressBar = (ProgressBar) itemView.findViewById(viewId);
        progressBar.setMax(100);
        progressBar.setProgress(progress);

//        ObjectAnimator animation;
//        if (progress > 100) {
//            animation = ObjectAnimator.ofInt(progressBar, "progress", 0, 100);
//        } else {
//            animation = ObjectAnimator.ofInt(progressBar, "progress", 0, progress);
//        }
//        animation.setDuration(1000);
//        if (!animation.isRunning()) {
//            animation.start();
//        }
    }

    public void setBackgroundColorRes(@IdRes int viewId, @ColorRes int colorRes) {
        itemView.findViewById(viewId).setBackgroundResource(colorRes);
    }

    public void setBackgroundDrawableRes(@IdRes int viewId, @DrawableRes int drawableRes) {
        itemView.findViewById(viewId).setBackgroundResource(drawableRes);
    }

    public void setBackGroundDrawable(@IdRes int viewId, Drawable drawable) {
        itemView.findViewById(viewId).setBackgroundDrawable(drawable);
    }

    public void setSelected(@IdRes int viewId, boolean selected) {
        itemView.findViewById(viewId).setSelected(selected);
    }
}
